package com.clb.toolsbox.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

/**
 * author : 常利兵
 * github : <a href="https://github.com/dxmwl">github</a>
 * time   : 2025/6/7 17:16
 * file   : null.java
 * desc   :
 */
public class ToolsUtils {
    private JPanel createJsonFormatter() {
        JPanel panel = new JPanel(new BorderLayout());
        JTextArea input = new JTextArea("输入 JSON...");
        JButton formatButton = new JButton("格式化");
        JTextArea output = new JTextArea();
        output.setEditable(false);

        formatButton.addActionListener(e -> {
            try {
                ObjectMapper mapper = new ObjectMapper();
                JsonNode jsonNode = mapper.readTree(input.getText());
                output.setText(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode));
            } catch (Exception ex) {
                output.setText("无效的 JSON 格式");
            }
        });

        panel.add(new JScrollPane(input), BorderLayout.NORTH);
        panel.add(formatButton, BorderLayout.CENTER);
        panel.add(new JScrollPane(output), BorderLayout.SOUTH);
        return panel;
    }

    private JPanel createBase64EncoderDecoder() {
        JPanel panel = new JPanel(new BorderLayout());
        JTextArea input = new JTextArea("输入文本...");
        JButton encodeButton = new JButton("编码");
        JButton decodeButton = new JButton("解码");
        JTextArea output = new JTextArea();
        output.setEditable(false);

        encodeButton.addActionListener(e -> {
            String text = input.getText();
            output.setText(Base64.getEncoder().encodeToString(text.getBytes()));
        });

        decodeButton.addActionListener(e -> {
            try {
                byte[] decodedBytes = Base64.getDecoder().decode(input.getText());
                output.setText(new String(decodedBytes));
            } catch (IllegalArgumentException ex) {
                output.setText("无效的 Base64 字符串");
            }
        });

        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(encodeButton);
        buttonPanel.add(decodeButton);

        panel.add(new JScrollPane(input), BorderLayout.NORTH);
        panel.add(buttonPanel, BorderLayout.CENTER);
        panel.add(new JScrollPane(output), BorderLayout.SOUTH);
        return panel;
    }

    private JPanel createTimestampConverter() {
        JPanel panel = new JPanel(new BorderLayout());
        JTextField timestampField = new JTextField("输入时间戳...");
        JButton convertButton = new JButton("转换");
        JTextArea resultArea = new JTextArea();
        resultArea.setEditable(false);

        convertButton.addActionListener(e -> {
            try {
                long timestamp = Long.parseLong(timestampField.getText());
                Date date = new Date(timestamp);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                resultArea.setText(sdf.format(date));
            } catch (NumberFormatException ex) {
                resultArea.setText("无效的时间戳");
            }
        });

        panel.add(timestampField, BorderLayout.NORTH);
        panel.add(convertButton, BorderLayout.CENTER);
        panel.add(new JScrollPane(resultArea), BorderLayout.SOUTH);
        return panel;
    }

}
